package com.ruoyi.sea.common.task;

import com.ruoyi.sea.domain.SeaDevice;
import com.ruoyi.sea.domain.SeaValueDay;
import com.ruoyi.sea.domain.SeaValueMonth;
import com.ruoyi.sea.mapper.SeaDeviceMapper;
import com.ruoyi.sea.mapper.SeaValueDayMapper;
import com.ruoyi.sea.mapper.SeaValueMonthMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class SeaTask {
    @Autowired
    private SeaValueDayMapper seaValueDayMapper;
    @Autowired
    private SeaValueMonthMapper seaValueMonthMapper;
    @Autowired
    private SeaDeviceMapper seaDeviceMapper;

    // 定时任务获取当前日期前一天的所有数据求平均值，且相应保留小数位，插入到每日数据表
    public void scheduledTaskSeaBeforeDayData() {
        List<SeaDevice> deviceList = seaDeviceMapper.selectSeaDeviceList(new SeaDevice());
        if (deviceList != null && deviceList.size() > 0) {
            for (SeaDevice device : deviceList) {
                SeaValueDay dayAvgData = seaValueDayMapper.taskQueryAvgSeaDaysData(device.getCode());
                if (dayAvgData != null) {
                    dayAvgData.setDeviceCode(device.getCode());
                    dayAvgData.setCreateBy("admin");
                    seaValueDayMapper.insertSeaValueDay(dayAvgData);
                }
            }
        }
    }

    // 定时任务获取当前日期上个月的所有天数据求平均值，且相应保留小数位，插入到每月数据表
    public void taskQueryAvgSeaBeforeMonthData() throws ParseException {
        List<SeaDevice> deviceList = seaDeviceMapper.selectSeaDeviceList(new SeaDevice());
        // 有设备
        if (deviceList != null && deviceList.size() > 0) {
            for (SeaDevice device : deviceList) {
                SeaValueMonth monthData = seaValueMonthMapper.taskQueryAvgSeaMonthDaysData(device.getCode());
                if (monthData != null) {
                    monthData.setCreateTime(new Date());
                    monthData.setDeviceCode(device.getCode());// 设备编码
                    monthData.setCreateBy("admin");
                    // 年月格式化
                    String mdStr = monthData.getRemark();// 202503
                    SimpleDateFormat sToDate = new SimpleDateFormat("yyyyMM");
                    Date toDate = sToDate.parse(mdStr);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
                    String dateStr = sdf.format(toDate);
                    monthData.setRemark(dateStr);

                    int i = seaValueMonthMapper.insertSeaValueMonth(monthData);
                    if(i > 0){
                        // 创建HttpClient
                        CloseableHttpClient httpClient = HttpClients.createDefault();
                        HttpPost httpPost = new HttpPost("http://127.0.0.1/v1/workflows/run");
                        httpPost.setHeader("Authorization", "Bearer app-tK1N39twcp2YXTQhVaRBnE3E");
                        httpPost.setHeader("Content-Type", "application/json");
                        String json = "{\"inputs\": {\"tem\":\"" + monthData.getWaterTemperature() + "\"," +
                                "\"cod\":\"" + monthData.getCod() + "\"," +
                                "\"month\":\"" + monthData.getRemark() + "\"," +
                                "\"code\":\"" + monthData.getDeviceCode() + "\"," +
                                "\"ammonia_nitrogen\":\"" + monthData.getAmmoniaNitrogen() + "\"," +
                                "\"conductivity\":\"" + monthData.getConductivity() + "\"," +
                                "\"residual_chlorine\":\"" + monthData.getResidualChlorine() + "\"," +
                                "\"ph\":\"" + monthData.getPh() + "\"},\"response_mode\":\"blocking\",\"user\":\"lhd-1234\"}";
                        System.out.println(json);
                        httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
                        try {
                            HttpResponse response = httpClient.execute(httpPost);
                            String responseString = EntityUtils.toString(response.getEntity());
                            System.out.println("Response: " + responseString);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
}
